const puppeteer = require("puppeteer");
const fs = require("fs");
const { log } = require("console");

(async () => {
  let data = [];

  const browser = await puppeteer.launch({
    headless: false,
    userDataDir: "./data",
  });
  const page = await browser.newPage();

  await page.goto(
    "https://www.bilibili.com/video/BV1ym42177Hy/?spm_id_from=333.1007.top_right_bar_window_dynamic.content.click&vd_source=3fb379bdd5d3e5bd63fada6189f6d4b6"
  );

  await page.waitForSelector("div > div > div > div.reply-warp > div.reply-list > div > div.root-reply-container > div.content-warp > div.root-reply > span > span");

  let titles = [];
  let previousHeight;
  let loop = 0;//内容循环次数
  let h = 50;

  while (loop < h) {
    const currentTitles = await page.$$eval(
      "div > div > div > div.reply-warp > div.reply-list > div > div.root-reply-container > div.content-warp > div.root-reply > span > span",
      (links) => links.map((x) => x.innerText)
    );

    if (currentTitles.length === 0) {
      break;
    }

    titles = titles.concat(currentTitles);

    previousHeight = await page.evaluate('document.body.scrollHeight');
    await page.evaluate('window.scrollTo(0, document.body.scrollHeight)');
    await page.waitForTimeout(1000); // 等待页面加载新内容
    loop++;
    console.log(`${loop}/${h}`);
  }

  console.log(titles);
  data = data.concat(titles);

  fs.writeFile("data.json", JSON.stringify(data, null, "\t"), function (err) {
    if (err) {
      console.log(err);
    }
  });

  await browser.close();
})();
